
/** 
* @Description: (用一句话描述该文件做什么) 
* @author heliang
* @date 2019-8-29 下午2:21:23 
* @version Bate1.0
*/

package org.alipay4j.component.life;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alipay4j.Alipay;
import org.alipay4j.AlipayException;
import org.alipay4j.Configuration;
import org.alipay4j.component.AbstractComponent;
import org.alipay4j.http.AlipayHttpsClient;
import org.alipay4j.http.Response;
import org.alipay4j.model.advert.Advert;
import org.alipay4j.model.advert.AdvertItem;
import org.alipay4j.util.ObjectHelper;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


/** 
 * @ClassName: 广告位操作组件 
 * @Description: 广告位操作组件
 * @author heliang
 * @date 2019-8-29 下午2:21:23 
 * @version V2.1 * Update Logs: * Name: * Date: * Description: 初始化
 */
public class AdvertComponent extends AbstractComponent {

    public AdvertComponent(Alipay alipay) {
        super(alipay);
    }
    
    /**
    * @Title: 生活号广告位添加接口 
    * @Description: 可通过本接口在生活号主页添加广告位，根据业务需求配置不同的图片和跳转链接
    * @author heliang 
    * @param advert_items 广告位轮播内容列表，数量限制：大于1个，小于5个，广告位轮播内容顺序，根据接口传入的顺序排列
    * @return advert_id  广告位id
    * @throws AlipayException 
     */
    public String create(List<AdvertItem> advert_items) throws AlipayException {
        if (ObjectHelper.isEmpty(advert_items)) {
            throw new IllegalArgumentException("advert_items can't be null or empty");
        }
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("advert_items", JSONObject.toJSONString(advert_items));
        body.put("method", "alipay.open.public.advert.create");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_advert_create_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_advert_create_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return bodyRes.getString("advert_id");
    }
    
    /**
    * @Title: 生活号广告位修改接口 
    * @Description: 可以通过此接口修改指定广告位的配置信息
    * @author heliang
    * @param advert_id 广告位id 
    * @param advert_items 广告位轮播内容列表，数量限制：大于1个，小于3个，广告位轮播内容顺序，根据接口传入的顺序排列
    * @return boolean
    * @throws AlipayException 
     */
    public boolean modify(String advert_id,List<AdvertItem> advert_items) throws AlipayException {
        if (ObjectHelper.isEmpty(advert_id)) {
            throw new IllegalArgumentException("advert_id can't be null or empty");
        }
        if (ObjectHelper.isEmpty(advert_items)) {
            throw new IllegalArgumentException("advert_items can't be null or empty");
        }
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("advert_id", advert_id);
        body.put("advert_items", JSONObject.toJSONString(advert_items));
        body.put("method", "alipay.open.public.advert.modify");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_advert_modify_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_advert_modify_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return true;
    }
    
    /**
    * @Title: 生活号广告位查询接口 
    * @Description:  可以通过此接口查询所有广告位的配置信息
    * @author heliang
    * @return List<Advert>
    * @throws AlipayException 
     */
    public List<Advert> batchquery() throws AlipayException {
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("method", "alipay.open.public.advert.batchquery");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_advert_batchquery_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_advert_batchquery_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        List<Advert> adverts = new ArrayList<Advert>();
        JSONArray array = bodyRes.getJSONArray("advert_list");
        for(int i=0;i<array.size();i++) {
            Advert advert = JSONObject.toJavaObject(array.getJSONArray(i), Advert.class);
            adverts.add(advert);
        }
        return adverts;
    }
    
    /**
    * @Title: 广告位删除 
    * @Description: 可以通过此接口删除指定广告位的配置信息
    * @author heliang
    * @param advert_id 广告位id
    * @return boolean
    * @throws AlipayException 
     */
    public boolean delete(String advert_id) throws AlipayException {
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("advert_id", advert_id);
        body.put("method", "alipay.open.public.advert.delete");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_advert_delete_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_advert_delete_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return true;
    }

}
